package com.tiemei.demo.client.udc;

import static com.tiemei.demo.util.Print.p;

import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.taobao.hsf.hsfunit.HSFEasyStarter;
import com.taobao.hsf.hsfunit.util.ServiceUtil;
import com.taobao.udc.client.UserDataReadService;
import com.taobao.udc.client.UserDataWriterService;
import com.taobao.udc.client.dataobject.DataObject;
import com.taobao.udc.client.enums.AppName;

/**
 * udc client
 * Run directly!
 *
 * @author tiemei.jdk
 * @date   2012-9-29
 */
public class UdcClient implements Serializable {
	/** serialVersionUID */
    private static final long serialVersionUID = 1L;

	public static void main (String args[]) {
//		putKeys(201209292L, new HashMap<String, Object>(){/** serialVersionUID */
//            private static final long serialVersionUID = 1L;
//		/** serialVersionUID */
//		{
////			put("sna", (Object)"铁眉店铺2");
//			put("st", 1);
//		}});
		putByTitleEn(201209293L, new HashMap<String, Object>(){
		/** serialVersionUID */
		{
//			put("sna", (Object)"铁眉店铺2");
//			put("seller_type", 1);
		}});
		
		showAllByUserId(201209293L);
//		showAllByUserId(201209292);
//		showAllByUserId(201209291);
	}
	
	
	
	/**
	 * 查询并打印单个用户信息
	 * 某个字段返回null，输出""
	 * @param userId
	 */
	private static void showAllByUserId(long userId){
		/*
		 *  因为测试环境下响应比较慢，不能调用多字段同时查询接口
		 */
		p("近30天支付宝成交量 :\t" +getKey("tc", userId));
		p("店铺名 :\t" +getKey("sna", userId));
		p("卖家类型 :\t" +getKey("st", userId));
		p("正品保障 :\t" +getKey("qpe", userId));
		p("消保 :\t" +getKey("ice", userId));
		p("如实描述 :\t" +getKey("ad", userId));
		p("假一赔三 :\t" +getKey("iatif", userId));
		p("七天无理由退换 :\t" +getKey("irwr", userId));
		p("24小时无理由退换 :\t" +getKey("hsdfh", userId));
		p("数码与家电30天维修 :\t" +getKey("idm", userId));
	}
	
	private static UserDataReadService reader;
	private static UserDataWriterService writer;
	private static final String APPID = "trident";
	
	static {
		HSFEasyStarter.start("d:/hsf/release", "");
		ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-hsf-udc.xml");
		reader = (UserDataReadService) ctx.getBean("userDataReadService");
		writer = (UserDataWriterService) ctx.getBean("userDataWriterService");
		ServiceUtil.waitServiceReady(reader, writer);//不是必须，因为运行很快，所以要在configServer推地址下来前检测地址是否推下來，可以自己sleep(500)
		p("server client ready!-------------------");
	};
	
	private static final String getKey (String key, long userId) {
		DataObject result = reader.getDataByKey(userId, key, false, APPID).getModel();
		return parseDataObject(result);
	}
	
	private static final void putKeys (long userId, Map<String, Object> values) {
		writer.putData(AppName.APP_UDC, userId, null, values);
	}
	private static final void putByTitleEn (long userId, Map<String, Object> values) {
		writer.putByTitleEn(AppName.APP_UDC, userId, null, values);
	}
	
	private static final String parseDataObject (DataObject dataObj) {
		Object value = dataObj == null ? "" : dataObj.getValue();
		return value == null ? "" : value.toString();
	}
}
